// import { null } from "mathjs";
import Vue from "vue";
import Vuex from "vuex";
import theme from "./modules/theme";

Vue.use(Vuex);

Vue.component("IndexSlider", () => import("@/components/index/IndexSlider")); // 侧边
Vue.component("IndexHeader", () => import("@/components/index/IndexHeader")); // 头部
Vue.component("IndexBreadcrumb", () => import("@/components/index/IndexBreadcrumb")); // 面包屑
Vue.component("GlobalTitle", () => import("@/components/GlobalTitle")); // 全局大标题

// 注册全局组件

export default new Vuex.Store({
  state: {
    userInfo: {},
    token: null,
    permissionList: [], // 权限列表
    menuList: [], // 侧边栏
    permissionNameList: [], // 权限名列表
    dictCodeList: {
    },
    operationTempType: {
    },
    operationTempStatus: {
      in: {code: 1, value: "操作中"},
      success: {code: 2, value: "成功"},
      fail: {code: 3, value: "失败"}
    },
    systemConfig: {
      form: {
        id: null,
        minVoltage: null,
        maxVoltage: null,
        minTemperature: null,
        maxTemperature: null,
        leftElectric: null,
        rightElectric: null,
        brushElectric: null,
        powerVoltagTransformUnder: null,
        powerVoltagTransformOver: null,
        voltage1: null,
        voltage2: null,
        originPoint: null,
        canCleanPower: null,
        cleanBaseTimeSlot: null,
        otherBaseTimeSlot: null,
      },
      // 步骤条 1-待获取 2-获取失败 3-配置中 4-配置失败 5-配置成功
      stepActive: 1,
    },
    socketBol: true, // false - 断开链接, true - 正常
    hasNewNotify: false, // 是否有新消息
  },
  mutations: {
    changeSocketBol(state, bol) {
      state.socketBol = bol;
    },
    saveToken(state, token) {
      state.token = token;
    },
    saveUserInfo(state, userInfo) {
      // console.log("-------注册")
      // console.log(1111, 'saveUserInfo')
      state.userInfo = userInfo;
    },
    savePermissionList(state, permissionList) {
      state.permissionList = permissionList;
    },
    saveOldIndexRouterList(state, oldIndexRouterList) {
      state.oldIndexRouterList = oldIndexRouterList;
    },
    saveNewIndexRouterList(state, newIndexRouterList) {
      state.newIndexRouterList = newIndexRouterList;
    },
    savePermissionNameList(state, permissionNameList) {
      state.permissionNameList = permissionNameList;
    },
    setSocket(state, socket) {
      state.socket = socket;
  },
  change_isRead(state,value){
    state.isRead = value
    state.hasNewNotify = value
    console.log("---1-----", state)
  }
  },
  actions: {
    initializeSocket({ state, commit }) {
      console.log("state====", state)
      const token = state.token;
      if (!token) return;
      // console.log("VUE_APP_WEBSOCKET_URL===", process.env.VUE_APP_WEBSOCKET_URL)
      // const wsUrl = `ws://192.168.110.156:9003/checkorder/websocket/${token}/2`;
      // const socket = new WebSocket(wsUrl);
      //const socket = new WebSocket(process.env.VUE_APP_WEBSOCKET_URL + "/checkorder/websocket/" + token + "/2");

        // 绑定事件
        // socket.onopen = () => {
        //   console.log("WebSocket 连接成功");
        //   commit("changeSocketBol", true);
        // };
  
        // socket.onmessage = (event) => {
        //   console.log("收到消息:", event.data);
        //   commit("change_isRead", true); // 更新 isRead 状态
        //   Vue.prototype.$bus.$emit("websocket-message", event.data);
        // };

        // socket.onclose = () => {
        //   console.log('WebSocket连接已关闭');
        //   commit("changeSocketBol", false);
        //   setTimeout(() => { //自动重连
        //     this.dispatch("initializeSocket");
        //   }, 5000);
        // };

        // socket.onerror = (error) => {
        //   console.error("WebSocket 错误:", error);
        //   commit("changeSocketBol", false);
        // };

        // commit("setSocket", socket);
  
    },

    handleChangeisRead({commit},value){
      commit('change_isRead',value)
    },
  },
  getters: {
    getSocket: state => state.socket
},
  modules: {
    theme
  }
});
